package com.jz;

import com.jz.model.UserVo;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.ws.rs.POST;

/**
 * Created by Hasee on 2022-02-23.
 */
@RestController
@DefaultProperties(defaultFallback = "defaultJiangJi")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProviderClient providerClient;

    @PostMapping("consumerPost")
    public String consumerPost(@RequestBody UserVo userVo){
        String forObject = restTemplate.postForObject("http://provider123/postProvider",userVo,String.class);
        return forObject;
    }
    //每台机器轮询三次，在切换机器。
//    private static int count = 1;
//    private static int jishu = 1;

    @GetMapping("consumerFeign")
   /* @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//设置熔断机制
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//短路期的最小请求数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "1000"),//时间窗口
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//百分比
    })*/
    @HystrixCommand
    public ResultVo consumerFeign(Integer count)throws Exception{
        if (count==1){
            throw new Exception();
        }else {
            return ResultBuilderVo.success("成功！！！！");
        }
//        String s = providerClient.feignProvider();
    }
    /*@GetMapping("consumerFeign")
    @HystrixCommand(fallbackMethod = "fuwujiangji")
    public ResultVo consumerFeign(){
        String s = providerClient.feignProvider();
        return ResultBuilderVo.success(s);
    }*/
    public ResultVo fuwujiangji(){
        return ResultBuilderVo.errorTan("跨服务访问异常，请稍后重试！");
    }
    @GetMapping("consumerFeign22")
    @HystrixCommand
    public ResultVo consumerFeign22()throws Exception{
       throw new Exception();
    }

    public ResultVo defaultJiangJi(){
        return ResultBuilderVo.errorTan("默认服务降级，跨服务访问异常，请稍后重试！");
    }

    @GetMapping("consumerFeign2")
    @HystrixCommand
    public String consumerFeign2(Integer id,String name){
        String s = providerClient.testProvider(id,name);
        return s;
    }
    @PostMapping("consumerFeign3")
    @HystrixCommand
    public String consumerFeign3(@RequestBody UserVo userVo){
        String s = providerClient.postProvider(userVo);
        return s;
    }
    /*
      @GetMapping("consumer")
    public String consumer(){
       if (jishu%3==0){
              count++;
          }
          jishu++;
        if (count%2==0){
            String forObject = restTemplate.getForObject("http://localhost:8010/provider", String.class);
            return forObject;
        }else {
            String forObject = restTemplate.getForObject("http://localhost:8011/provider", String.class);
            return forObject;
        }
    }*/
   /* @GetMapping("consumer")
    public String consumer(){
        if (count%2==0){
            String forObject = restTemplate.getForObject("http://localhost:8010/provider", String.class);
            count++;
            return forObject;
        }else {
            String forObject = restTemplate.getForObject("http://localhost:8011/provider", String.class);
            count++;
            return forObject;
        }
    }*/

}
